package ghr.day0109.travelt.vo;


import ghr.day0109.travelt.dao.RelationDao;
import ghr.day0109.travelt.dao.RouteDao;
import ghr.day0109.travelt.dao.ScenicDao;
import ghr.day0109.travelt.dao.impl.RelationDaoImpl;
import ghr.day0109.travelt.dao.impl.RouteDaoImpl;
import ghr.day0109.travelt.dao.impl.ScenicDaoImpl;
import ghr.day0109.travelt.entity.ScenicSpot;
import ghr.day0109.travelt.entity.TourRoute;
import ghr.day0109.travelt.entity.TourScenicRelation;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class Travel {
    ScenicDao scenicDao = new ScenicDaoImpl();
    RouteDao routeDao = new RouteDaoImpl();
    RelationDao relationDao = new RelationDaoImpl();

    public void showScenic() {
        List<ScenicSpot> spotList = scenicDao.queryAllScenic();
        System.out.println("景点\t\t游玩时长\t\t介绍");
        for (ScenicSpot spotSpot : spotList) {
            System.out.println(spotSpot.getSsName() + "\t\t" + spotSpot.getSsDuration() + "\t\t\t\t"
                    + spotSpot.getSsContent());
        }

    }

    public void showRoute() {
        List<TourRoute> routeList = routeDao.queryAllTourRoute();
        System.out.println("旅游路线名称\t\t类型\t\t价格\t\t咨询电话\t\t咨询人\t\t创建日期\t\t包含景点");
        String type = "";
        List ssId = new ArrayList();
        List scenic = new ArrayList();
        for (TourRoute route : routeList) {
            if (route.getTrType() == 1) {
                type = "自由行";
            } else {
                type = "纯玩团";
            }
            List<TourScenicRelation> relationList = relationDao.queryOneRelation(String.valueOf(route.getTrID()));
            for (TourScenicRelation relation : relationList) {
                ssId.add(relation.getSsId());
            }
            for (int i = 0; i < ssId.size(); i++) {
                List<ScenicSpot> spotList = scenicDao.queryOneScenic(String.valueOf(ssId.get(i)));
                for (ScenicSpot spotSpot : spotList) {
                    scenic.add(spotSpot.getSsName());
                }
            }
            System.out.println(route.getTrName() + "\t\t" + type + "\t\t" + route.getTrPrice() + "\t\t"
                    + route.getTrPhone() + "\t\t" + route.getTrUser() + "\t\t" + route.getCreateTime() + "\t\t"
                    + StringUtils.strip(scenic.toString(), "[]"));
            ssId.clear();
            scenic.clear();
        }
    }

    public void add(String name, int type, int price, String phone, String user, String ssId) {
        TourRoute tourRoute = new TourRoute();
        String[] arr = ssId.split(",");
        int routeRes = 0, relationRes = 0;
        tourRoute.setTrName(name);
        tourRoute.setTrType(type);
        tourRoute.setTrPrice(price);
        tourRoute.setTrPhone(phone);
        tourRoute.setTrUser(user);
        routeRes = routeDao.addRoute(tourRoute);
        for (int i = 0; i < arr.length; i++) {
            TourScenicRelation relation = new TourScenicRelation();
            List<TourRoute> routeList = routeDao.queryOneTourRoute(name);
            for (TourRoute route : routeList) {
                relation.setTrId(String.valueOf(route.getTrID()));
            }
            relation.setSsId(arr[i]);
            relationRes = relationDao.addRelation(relation);
        }
        System.out.println(routeRes > 0 && relationRes > 0 ? "成功" : "失败");
    }

    public void selectRoute(String name){
        List<TourRoute> routeList = routeDao.queryOneTourRoute(name);
        System.out.println("旅游路线名称\t\t类型\t\t价格\t\t咨询电话\t\t咨询人\t\t创建日期");
        String type = "";
        List ssId = new ArrayList();
        List scenic = new ArrayList();
        for (TourRoute route : routeList) {
            if (route.getTrType() == 1) {
                type = "自由行";
            } else {
                type = "纯玩团";
            }
            List<TourScenicRelation> relationList = relationDao.queryOneRelation(String.valueOf(route.getTrID()));
            for (TourScenicRelation relation : relationList) {
                ssId.add(relation.getSsId());
            }
            System.out.println(route.getTrName() + "\t\t" + type + "\t\t" + route.getTrPrice() + "\t\t"
                    + route.getTrPhone() + "\t\t" + route.getTrUser() + "\t\t" + route.getCreateTime() + "\t\t"
                    );
            System.out.println("景点\t\t游玩时长\t\t介绍");
            for (int i = 0; i < ssId.size(); i++) {
                List<ScenicSpot> spotList = scenicDao.queryOneScenic(String.valueOf(ssId.get(i)));
                for (ScenicSpot spotSpot : spotList) {
                    System.out.println(spotSpot.getSsName() + "\t\t" + spotSpot.getSsDuration() + "\t\t\t\t"
                            + spotSpot.getSsContent());
                }
            }
            ssId.clear();
            scenic.clear();
        }
    }

    public void deleteRoute(String id) {
        TourScenicRelation relation = new TourScenicRelation();
        TourRoute route = new TourRoute();
        relation.setTrId(id);
        route.setTrID(Integer.parseInt(id));
        int routeRes = routeDao.deleteRoute(route);
        int relationRes = relationDao.deleteRelation(relation);
        System.out.println(routeRes > 0 && relationRes > 0 ? "成功" : "失败");
    }
}
